package com.teen9g.infoplatformserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teen9g.entity.ChatMessage;
import com.teen9g.vo.ChatMessageSideVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChatMapper extends BaseMapper<ChatMessage> {

    /**
     * 获取用户之间的聊天历史记录
     *
     * @param userId1 用户ID1
     * @param userId2 用户ID2
     * @return 聊天历史记录列表
     */
    @Select("select * from chat_message where receiver_id = #{userId1} and sender_id = #{userId2} " +
            "or receiver_id = #{userId2} and sender_id = #{userId1} order by send_time")
    List<ChatMessage> getChatHistory(Integer userId1, Integer userId2);

    /**
     * 获取用户的聊天历史记录列表（左侧列表）不带头像
     *
     * @param userId 用户ID
     * @return 聊天历史记录列表
     * - 通过子查询找到每个用户的最新时间戳，然后通过INNER JOIN将这些时间戳与原表匹配，最终筛选出每个用户的最新记录。
     */
    List<ChatMessageSideVO> getChatHistoryList(@Param("userId") Integer userId);

    /**
     * 删除用户之间的聊天记录 userId1单方面删除userId2的聊天记录
     *
     * @param userId1 用户ID1
     * @param userId2 用户ID2
     * @return 操作结果
     */
    @Update("update chat_message set " +
            "deleted_by_sender = IF(sender_id = #{userId1}, true, deleted_by_sender)," +
            "deleted_by_receiver = IF(receiver_id = #{userId1}, true, deleted_by_receiver) " +
            "where (sender_id = #{userId1} AND receiver_id = #{userId2}) OR " +
            "(sender_id = #{userId2} AND receiver_id = #{userId1})")
    int deleteChat(@Param("userId1") Integer userId1, @Param("userId2") Integer userId2);

    /**
     * 根据搜索条件获取用户的聊天历史记录列表 不带头像
     * 用于左侧列表的搜索功能
     *
     * @param content 搜索内容
     * @return 聊天历史记录列表
     */
    @Select("select u1.* from chat_message u1 inner join " +
            "(select max(send_time) as send_time, sender_id from chat_message " +
            "group by sender_id) u2 on u1.send_time = u2.send_time and u1.sender_id = u2.sender_id " +
            "where u1.message_content like CONCAT('%', #{content}, '%') and " +
            "(u1.receiver_id = #{userId} or u1.sender_id = #{userId})")
    List<ChatMessageSideVO> getChatHistoryListBySearch(@Param("userId") Integer userId,
                                                       @Param("content") String content);

    @Select("select * from chat_message where chat_id = #{chatId} order by send_time")
    List<ChatMessage> getChatHistoryByChatId(String chatId);

    @Select("select chat_id from chat_message " +
            "where sender_id = #{userId} or receiver_id = #{userId} group by chat_id")
    List<String> getChatIds(Integer userId);

    @Insert("insert into chat_message_ai " +
            "(chat_id, sender_id, sender_name, receiver_id, receiver_name, message_content, message_status, deleted_by_receiver, deleted_by_sender, send_time) " +
            "values (#{chatId}, #{senderId}, #{senderName}, #{receiverId}, #{receiverName}, #{messageContent}, #{messageStatus}, #{deletedByReceiver}, #{deletedBySender}, #{sendTime})")
    void insertWithAI(ChatMessage chatMessage);

    @Select("select * from chat_message_ai where chat_id = #{chatId} order by send_time")
    List<ChatMessage> getAIChatHistoryByChatId(String chatId);

    @Insert("insert into chat_message_ai " +
            "(chat_id, sender_id, sender_name, receiver_id, receiver_name, message_content, message_status, deleted_by_sender, deleted_by_receiver, send_time) " +
            "values (#{chatId}, #{senderId}, #{senderName}, #{receiverId}, #{receiverName}, #{messageContent}, #{messageStatus}, #{deletedByReceiver}, #{deletedBySender}, #{sendTime})")
    void saveAIMessage(ChatMessage aiMessage);

    void updateReadStatus(List<Integer> messageIds);
}
